<!--上传验收单-->
<template>
  <edit-dialog :centerDialogVisible="visible" :tilte="title" width="400" @Cancel="handleCancel" @Submit="handleSubmit" :disabled="loading">
    <template #content>
      <el-form :model="form" :rules="rules" ref="applyApproveFormRef" class="upload-approve-form" label-width="auto">
        <el-form-item label="上传文件" prop="files">
          <HyUpload v-model:file-list="form.files" @validateField="validateField"/>
        </el-form-item>
        <el-form-item label="是否退场" prop="isExit">
          <el-radio-group v-model="form.isExit" placeholder="请选择" style="width: 100%;">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="退场时间" prop="exitTime" v-if="form.isExit === 1">
          <el-date-picker v-model="form.exitTime" placeholder="请选择" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss"
                          style="width: 100%;"/>
        </el-form-item>
      </el-form>
    </template>
  </edit-dialog>
</template>

<script>
import EditDialog from "@/components/editDialog/index.vue";
import ApplyApproveUploadFile from "@/views/construction/project-application/review-components/ApplyApproveUploadFile";
import {ApplyApproveType, stateEnum} from "@/views/construction/project-application/projectUtil";
import store from "@/store";
import HyUpload from "@/views/construction/project-application/componens/HyUpload";
import {saveFileRelate} from "@/api/common";
import {applyUploadDepositrefund} from "@/api/construction-project/projectManage";

export default {
  name: "ApplyUploadDepositrefundDialog",
  components: {HyUpload, ApplyApproveUploadFile, EditDialog},
  props: {
    options: {
      type: Object,
      default: () => ({})
    }
  },
  inject: ["options"],
  data() {
    return {
      visible: false,
      form: {
        type: Object,
        default: () => ({})
      },
      rules: {
        exitTime: [
          {required: true, message: '请选择', trigger: ['blur', 'change']},
        ],
        isExit: [
          {required: true, message: '请选择', trigger: ['blur', 'change']},
        ],
        files: [
          {required: true, message: '请上传', trigger: ['blur', 'change']},
        ]
      },
      title: '上传验收单',
      formType: undefined,
      ApplyApproveType,
      loading:false
    }
  },
  computed: {
    userInfo() {
      return store.state.user.userInfo
    },
  },
  methods: {
    open({row, formType, resolve}) {
      this.visible = true
      this.formType = formType
      this.resolve = resolve
      this.form = {
        status: 1,
        appllyPerson: {
          id: this.userInfo.userId,
          name: this.userInfo.name,
          organId: this.userInfo.organId,
          organName: this.userInfo.organName,
        },
        projectId: row.id,
        exitTime: undefined,
        isExit: 0,
        files: [],
      }
    },
    handleCancel() {
      this.$refs.applyApproveFormRef.resetFields()
      this.visible = false
    },
    handleSubmit() {
      let that = this
      this.$refs.applyApproveFormRef.validate(valid => {
        if (valid) {
          this.loading=true
          saveFileRelate({
            infoCode: that.form.projectId,
            moduleCode: stateEnum.WAIT_DEPOSIREFUND,
            enclosureCodeList: that.form.files
          })

          applyUploadDepositrefund(that.form).then(res => {
            that.$message.success('成功上传')
            that.resolve()
            that.visible = false
          }).catch(e => {
            this.$message.error(e)
          }).finally(()=>{
            this.loading=false
          })
        }
      })
    },
    validateField() {
      this.$refs.applyApproveFormRef.validateField('files')
    }
  }
}
</script>

<style lang="less" scoped>
.upload-approve-form {
  .title {
    margin-bottom: 10px;
  }

  .tip {
    color: #b6b6b6;
    margin: 10px 0;
  }
}
</style>
